/**
 *Express.js基本示例 
 */
// 导入Express模块
const express=require("express");
//导入cookie模块
const cookieParser = require("cookie-parser");
// 创建express应用
const app=express();

// Express http服务端口
const port=3001;

app.use(express.json());
app.use(express.urlencoded({extended:false}));

app.use(cookieParser());

app.get("/",function(req,res){
    if(req.cookies.remember){
        res.send(`记住了。<a href="/forget">忘记</a>!`);
    }else{
        let str =`
        <from method="post">
            <label>
                <input type="checkbox" name="remember"/>记住
            </label>
            <input type="submit" value="提交"/>
        </from>
        `;
        res.send(str);
    }
});
app.get("/forget",function(req,res){
    res.clearCookie("remember");
    res.redirect("back");
});

app.post("/",function(req,res){
    let minute = 6000;
    if(req.body.remember)
        res.cookie("remember",1,{maxAge: minute});
    res.redirect("back");
});


// 启动Express服务
app.listen(port,function(){
    console.log(`Myapp访问地址:http://127.0.0.1:${port}`);
});